package com.brkj.course;

import java.io.Serializable;
import java.util.List;

import android.graphics.BitmapFactory;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import com.brkj.download.CourseDownloader;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.brkj.model.DS_Course;
import com.brkj.main.R;
import com.brkj.util.ConstAnts;
import com.brkj.util.ImgShow;
import com.brkj.util.TimeHelp;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CourseAdapter extends BaseAdapter{
    protected ListItemView listItemView;
	protected LayoutInflater listContainer;
    protected Context context;
    protected List<DS_Course> courseList;
    protected FinalDb courseSave_db;

    public CourseAdapter(){};
    
	public CourseAdapter(Context context, List<DS_Course> courseList){
		this.context = context;
		this.courseList = courseList;
		
		listContainer = LayoutInflater.from(context);
		
		courseSave_db = FinalDb.create(context, ConstAnts.BRKJ_DB);
	}

    protected static class ListItemView{
		public ImageView CourseImg;
		public ImageView download;
		public TextView Title;
		public TextView Content;
		public TextView Time;
		public TextView StudyCount;
		public TextView teacher;
		public TextView course_time;
		
		
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return courseList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.course_item, null);
			// 获取控件对象
			listItemView.CourseImg = (ImageView) convertView.findViewById(R.id.CourseImg);
			listItemView.download = (ImageView) convertView.findViewById(R.id.download);
			listItemView.Title = (TextView) convertView
					.findViewById(R.id.Title);
			listItemView.Content = (TextView) convertView
					.findViewById(R.id.Content);
			listItemView.Time = (TextView) convertView
					.findViewById(R.id.Time);
			listItemView.StudyCount = (TextView) convertView
					.findViewById(R.id.StudyCount);
			listItemView.teacher = (TextView) convertView
					.findViewById(R.id.teacher);
			listItemView.course_time = (TextView) convertView
					.findViewById(R.id.course_time);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}
		
//		fb.display(listItemView.CourseImg, courseList.get(position).getCourseImg());
		ImgShow.display(listItemView.CourseImg, courseList.get(position).getCourseImg());
		
//		ImageLoader.getInstance().displayImage(
//				"http://118.192.9.79:7002/KMELearning//WebFiles/cwFiles/52266A266-BBFF-4482-A6A7-02196AB18763k.jpg"
//				, listItemView.CourseImg);
		listItemView.Title.setText(courseList.get(position).getTitle());
		listItemView.Content.setText(courseList.get(position).getContent());

        if(courseList.get(position).getCourseType() == DS_Course.VIDEO_COURSE){
            listItemView.Time.setVisibility(View.VISIBLE);
            listItemView.Time.setText("课程时长：" + courseList.get(position).getTime());
        }else{
            listItemView.Time.setVisibility(View.GONE);
        }

		listItemView.StudyCount.setText(
				"学习(" + courseList.get(position).getStudyCount()+ ")");
		listItemView.teacher.setText("讲师姓名：" + courseList.get(position).getTeacher());
		listItemView.course_time.setText(courseList.get(position).getSubmitTime());


        //如果是url课件， 隐藏下载
        if(courseList.get(position).getCourseType() == DS_Course.URL_COURSE){
            listItemView.download.setVisibility(View.GONE);
        }else{
            listItemView.download.setVisibility(View.VISIBLE);
            final int pos = position;
            listItemView.download.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    CourseDownloader downloader = CourseDownloader.Create(context);

                    if(downloader.Download(courseList.get(pos)) == 0) {
                        Intent intent = new Intent();
                        intent.setAction("com.dgl.startDownloadAnim");
                        int[] location = new  int[2] ;
                        v.getLocationInWindow(location);
                        intent.putExtra("xPos", location[0]);
                        intent.putExtra("yPos", location[1]);
                        context.sendBroadcast(intent);
                    }

                }
            });
        }

		convertView.setOnClickListener(new MyClickListener(position));
		
		return convertView;
	}

	class MyClickListener implements OnClickListener{
		private int position;
		
		public MyClickListener(int position){
			this.position = position;
		}
		@Override
		public void onClick(View v) {
			
			Intent intent = new Intent(context, CourseDetailActivity.class);
			
			intent.putExtra("course", courseList.get(position));
			context.startActivity(intent);
			
			List<DS_Course> savedList = courseSave_db.findAllByWhere(DS_Course.class, 
					" CourseID=" + Integer.toString(courseList.get(position).getCourseID())); 
			
			courseList.get(position).setStudyTime(TimeHelp.geLongTime());
			
			if((savedList!=null) && (savedList.size()>0)){
				courseSave_db.update(courseList.get(position), 
						" CourseID="+Integer.toString(courseList.get(position).getCourseID()));
			}else{
				courseSave_db.save(courseList.get(position));
			}
			
		}
		
	}
}
